<?php
namespace App\Api;

use PhalApi\Api;
use App\Domain\Matchs as Domain_Matchs;
use App\Domain\Vip as Domain_Vip;
/**
 * 匹配
 */

class Matchs extends Api {

	public function getRules() {
        return array(
            'getMatch' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
			),
            
            'check' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
                'type' => array('name' => 'type', 'type' => 'int', 'desc' => '类型，1视频2语音'),
			),
            
            'userMatch' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'type' => array('name' => 'type', 'type' => 'int', 'desc' => '类型，1视频2语音'),
			),
            
            'userCancel' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
			),
            
            'anchorMatch' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
                'type' => array('name' => 'type', 'type' => 'int', 'desc' => '类型，1视频2语音'),
			),
            
            'anchorCancel' => array(
				'uid' => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
			),
			
        );
	}
	
    /**
     * 匹配配置
     * @desc 用于获取匹配设置
     * @return int code 操作码，0表示成功
     * @return array info 
     * @return string info[].isauth 是否主播认证，0否1是
     * @return string info[].isuser_auth 是否实名认证，0否1是
     * @return string info[].voice 语音价格 
     * @return string info[].voice_vip 语音-VIP价格  
     * @return string info[].video 视频价格
     * @return string info[].video_vip 视频-VIP价格  
     * @return string msg 提示信息
     */
	public function getMatch() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $uid=\App\checkNull($this->uid);
		$token=\App\checkNull($this->token);
        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}
        
        $isauth=\App\isAuthorAuth($uid);
        $isuser_auth=\App\isAuth($uid);
        
        $configpri=\App\getConfigPri();
        
        $info=[
            'voice'=>$configpri['match_voice'],
            'voice_vip'=>$configpri['match_voice_vip'],
            'video'=>$configpri['match_video'],
            'video_vip'=>$configpri['match_video_vip'],
            'isauth'=>$isauth,
            'isuser_auth'=>$isuser_auth,
            'free_times'=>5,
        ];
        
        $rs['info'][0]=$info;
        
        return $rs;
	}

    /**
     * 匹配检测
     * @desc 用于用户[非主播]开始匹配前检测信息
     * @return int code 操作码，0表示成功
     * @return array info 
     * @return string msg 提示信息
     */
	public function check() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $uid=\App\checkNull($this->uid);
		$token=\App\checkNull($this->token);
		$type=\App\checkNull($this->type);
        
        if($uid<1 || $token=='' || ($type!=1 && $type !=2) ){
            $rs['code'] = 1001;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}
        
        /*$isauth=\App\isAuthorAuth($uid);
        if($isauth){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('您已通过主播认证，请返回首页重新匹配');
			return $rs;
        }*/
        
        $configpri=\App\getConfigPri();
        $fee=[
            '1'=>[
                '0'=>$configpri['match_video'],
                '1'=>$configpri['match_video_vip'],
            ],
            '2'=>[
                '0'=>$configpri['match_voice'],
                '1'=>$configpri['match_voice_vip'],
            ],
        ];
        

        $domain_vip = new Domain_Vip();  
        $uservip=$domain_vip->myVip($uid);
        $total=$fee[$type][$uservip['isvip']];
        
        if($total<=0){
            $rs['code'] = 1007;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        //$free = $this->GetMatch(['uid'=>$uid,'token'=>$token]);
        // Donx 临时注释，暂不计算免费匹配次数
        if(true){
            return $rs;
        }else{
            $userinfo=\App\getUserCoin($uid);
            if($userinfo['coin'] < $total*1){
                $rs['code'] = 1008;
                $rs['msg'] = \PhalApi\T('余额不足1分钟通话，请先充值');
                return $rs;
            }
        }
        return $rs;
	}

    /**
     * 用户匹配
     * @desc 用于用户开始匹配
     * @return int code 操作码，0表示成功
     * @return array info 
     * @return string msg 提示信息
     */
	public function userMatch() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $uid=\App\checkNull($this->uid);
		$token=\App\checkNull($this->token);
		$type=\App\checkNull($this->type);
        
        if($uid<1 || $token=='' || ($type!=1 && $type !=2) ){
            $rs['code'] = 1001;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}
        
        $configpri=\App\getConfigPri();
        
        $fee=[
            '1'=>[
                '0'=>$configpri['match_video'],
                '1'=>$configpri['match_video_vip'],
            ],
            '2'=>[
                '0'=>$configpri['match_voice'],
                '1'=>$configpri['match_voice_vip'],
            ],
        ];
        

        $domain_vip = new Domain_Vip();
        
        $uservip=$domain_vip->myVip($uid);
        
        $total=$fee[$type][$uservip['isvip']];
        
        if($total<=0){
            $rs['code'] = 1007;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        

        $userinfo=\App\getUserCoin($uid);
        // Donx 临时注释，暂不计算免费匹配次数
        if(true){
            
        }else{
            if($userinfo['coin'] < $total*1){
                $rs['code'] = 1008;
                $rs['msg'] = \PhalApi\T('余额不足1分钟通话，请先充值');
                return $rs;
            }
        }

        $key_user='match_user_'.$type; 
        $key_anchor='match_anchor_'.$type; 
        
        //$anchorid=\App\lPop($key_anchor);
        // Donx 临时注释
        // if(!$anchorid){
        //     \App\rPush($key_user,$uid);
        //     return $rs;
        // }
		//判断是否是拉黑主播
		/*$isBlack=\App\isBlack($uid,$anchorid);
		if($isBlack){
			return $rs;
		}*/
        $anchorid = $this->getAnchorid($uid);
        if(empty($anchorid) || $anchorid == $uid){
            $rs['code'] = 1009;
            $rs['msg'] = \PhalApi\T('未匹配到用户');
            return $rs;
        }
        //$anchorid =25;
        $nowtime=time();
        $showid=$nowtime;
        $data=[
            'uid'=>$uid,
            'liveuid'=>$anchorid,
            'type'=>1,
            'showid'=>$showid,
        ];
        $this->setIM($uid,$data);
        //$domain = new Domain_Matchs();
        //$result=$domain->match(152,25,1);
        // Donx 临时注释
        /*if($uid !=64){
            return;
        }
		$result=$domain->match(64,25,$type);*/
        
        return $rs;
	}
	
	function getAnchorid($uid){
	    ini_set("memory_limit", "256M");
	    $anchorid = \App\sPop('authorids');
	    /*if($anchorid == $uid || empty($anchorid)){
	        \App\sAdd('authorids',$uid);
	        $anchorid = $this->getAnchorid($uid);
	    }*/
	    return $anchorid;
	}
	
	/* 发送私信 */
	public function setIM($uid,$info) {
        /* 主播给用户发 */
        $anchorinfo=\App\getUserInfo($info['liveuid']);
        //print_r($anchorinfo);exit;
        $ext=[
            'method'=>'call',
            'action'=>'15',
            'type'=>$info['type'],
            'showid'=>$info['showid'],
            
            'id'=>$anchorinfo['id'],
            'avatar'=>$anchorinfo['avatar'],
            'user_nickname'=>$anchorinfo['user_nickname'],
        ];
        //file_put_contents("3333.txt",json_encode($ext));
        
        #构造高级接口所需参数
        $msg_content = array();
        //创建array 所需元素
        $msg_content_elem = array(
            'MsgType' => 'TIMCustomElem',       //自定义类型
            'MsgContent' => array(
                'Data' => json_encode($ext),
                'Desc' => '',
            )
        );
        //将创建的元素$msg_content_elem, 加入array $msg_content
        array_push($msg_content, $msg_content_elem);
        
        $account_id=(string)$info['liveuid'];
        $receiver=(string)$info['uid'];
        $api=\App\getTxRestApi();
        $ret = $api->openim_send_msg_custom($account_id, $receiver, $msg_content);
	}

    /**
     * 用户取消
     * @desc 用于用户取消配置
     * @return int code 操作码，0表示成功
     * @return array info 
     * @return string msg 提示信息
     */
	public function userCancel() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $uid=\App\checkNull($this->uid);
		$token=\App\checkNull($this->token);
        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}
        $key_user1='match_user_1';
        $key_user2='match_user_2';
        \App\lRem($key_user1,$uid);
        \App\lRem($key_user2,$uid);
        
        return $rs;
	}

    /**
     * 主播匹配
     * @desc 用于主播开始匹配
     * @return int code 操作码，0表示成功
     * @return array info 
     * @return string msg 提示信息
     */
	public function anchorMatch() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $uid=\App\checkNull($this->uid);
		$token=\App\checkNull($this->token);
		$type=\App\checkNull($this->type);
        
        if($uid<1 || $token=='' || ($type!=1 && $type !=2) ){
            $rs['code'] = 1001;
			$rs['msg'] = \PhalApi\T('信息错误');
			return $rs;
        }
        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}
        
        $isauth=\App\isAuthorAuth($uid);
        if(!$isauth){
            $rs['code']=1003;
			$rs['msg']=\PhalApi\T('您还未认证主播或主播认证还未通过');
			return $rs;
        }
        
        $domain = new Domain_Matchs();
        /* 检测开关 */
        $userinfo=$domain->getLiveuidInfo($uid);
        // if(!$userinfo['isdisturb']==1){
            // $rs['code'] = 1005;
			// $rs['msg'] = \PhalApi\T('您未开启视频接听');
			// return $rs;
        // }
        
        if($type==1 && !$userinfo['isvideo']){
            $rs['code'] = 1005;
			$rs['msg'] = \PhalApi\T('您未开启视频接听');
			return $rs;
        }
        
        if($type==2 && !$userinfo['isvoice']){
            $rs['code'] = 1006;
			$rs['msg'] = \PhalApi\T('您未开启语音接听');
			return $rs;
        }
        
        
        $key_user='match_user_'.$type; 
        $key_anchor='match_anchor_'.$type; 
        $userid=\App\lPop($key_user);
        if(!$userid){
			\App\rPush($key_anchor,$uid);
            return $rs;
        }
		
		
		//判断是否是拉黑用户
		$isBlack=\App\isBlack($uid,$userid);
		if($isBlack){
			return $rs;
		}
        
        
		$result=$domain->match($userid,$uid,$type);
        
        return $result;

	}

    /**
     * 主播取消
     * @desc 用于主播取消匹配
     * @return int code 操作码，0表示成功
     * @return array info 
     * @return string msg 提示信息
     */
	public function anchorCancel() {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());
        
        $uid=\App\checkNull($this->uid);
		$token=\App\checkNull($this->token);
        
        $checkToken=\App\checkToken($uid,$token);
		if($checkToken==700){
			$rs['code'] = $checkToken;
			$rs['msg'] = \PhalApi\T('您的登陆状态失效，请重新登陆！');
			return $rs;
		}
        
        $key_anchor1='match_anchor_1';
        $key_anchor2='match_anchor_2';
        \App\lRem($key_anchor1,$uid);
        \App\lRem($key_anchor2,$uid);
        
        return $rs;
	}

    protected function test(){
        
        $key_user1='match_user_1';
        $key_user2='match_user_2';
        $key_anchor1='match_anchor_1'; 
        $key_anchor2='match_anchor_2'; 
        
        $list11=\App\lRange($key_user1);
        $list12=\App\lRange($key_user2);
        $list21=\App\lRange($key_anchor1);
        $list22=\App\lRange($key_anchor2);
        
        $info=[
            'list11'=>$list11,
            'list12'=>$list12,
            'list21'=>$list21,
            'list22'=>$list22,
        ];
        
		
		
	
        return $info;
    }
	

    
    
    
    
    
}
